﻿package net.wangit.framework.expression;

import net.wangit.Row;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;
/**
 * 数据表达式
 * @author watson
 *
 */
public class DataExpression {

	private static final String PREFIX = "$D{";
	private static final String SUFFIX = "}";

	public DataExpression() {
	}

	public static final String resolve(String expression, Row row) {
		if (Checker.isEmpty(expression))
			return expression;
		StringBuffer result = new StringBuffer();
		String attribute;
		while ((attribute = StringUtil.find(expression, DataExpression.PREFIX,
				DataExpression.SUFFIX)) != null) {
			int index = expression.indexOf((new StringBuilder(
					DataExpression.PREFIX)).append(attribute).append(
					DataExpression.SUFFIX).toString());
			result.append(expression.substring(0, index)).append(
					getValue(attribute, row));
			expression = expression.substring(index + attribute.length()
					+ DataExpression.PREFIX.length()
					+ DataExpression.SUFFIX.length());
		}
		result.append(expression);
		return result.toString();
	}

	private static String getValue(String attribute, Row row) {
		if (row == null)
			return "";
		else
			return row.get(attribute, "");
	}
}
